from commons.entity.po.travel import Travel

from commons.entity.po.model import db


class Schedule(db.Model):
    __tablename__ = 'schedule'

    schedule_id = db.Column(db.Integer, primary_key=True, autoincrement=True, info='行程编号')
    location = db.Column(db.String(255), info='行程地点')
    task = db.Column(db.String(255), info='行程任务')
    schedule_detail = db.Column(db.String(255), info='行程详情')
    start_date = db.Column(db.Date, info='开始日期')
    end_time = db.Column(db.Date, info='结束日期')
    travel_id = db.Column(db.ForeignKey('travel.travel_id'), nullable=False, index=True, info=' 出差编号')

    travel = db.relationship('Travel', primaryjoin='Schedule.travel_id == Travel.travel_id', backref='schedules')

    def to_dict(self):
        return {
            'schedule_id': self.schedule_id,
            'location': self.location,
            'task': self.task,
            'schedule_detail': self.schedule_detail,
            'start_date': self.start_date.isoformat() if self.start_date else None,
            'end_time': self.end_time.isoformat() if self.end_time else None,
            'travel_id': self.travel_id
        }

    def to_dict_2(self):
        return {
            'scheduleId': self.schedule_id,
            'location': self.location,
            'task': self.task,
            'scheduleDetail': self.schedule_detail,
            'startDate': self.start_date.isoformat() if self.start_date else None,
            'endTime': self.end_time.isoformat() if self.end_time else None,
            'travelId': self.travel_id
        }
